import pygame
import sys
import os
import random
from utils.config import (
    SCREEN_WIDTH, SCREEN_HEIGHT, GAME_SPEED, BG_IMAGE,
    SNAKE_COLOR, FOOD_COLOR, BACKGROUND_COLOR, FONT_NAME
)

class EndInterface:
    def __init__(self, screen, width, height, score, high_score):
        self.screen = screen
        self.width = width
        self.height = height
        self.score = score
        self.high_score = high_score
        self.font = pygame.font.SysFont(FONT_NAME, 64)
        self.small_font = pygame.font.SysFont(FONT_NAME, 32)

    def show(self):
        running = True
        replay_rect = None
        exit_rect = None
        while running:
            self.screen.fill((0, 0, 0))
            y_center = self.height // 2
            if self.score >= self.high_score:
                high_score_surface = self.font.render("New High Score!", True, (255, 255, 0))
                high_score_rect = high_score_surface.get_rect(center=(self.width // 2, y_center - 90))
                self.screen.blit(high_score_surface, high_score_rect)
                msg = f"Game Over! Score: {self.score}"
                text = self.font.render(msg, True, (255, 255, 0))
                rect = text.get_rect(center=(self.width // 2, y_center - 40))
                self.screen.blit(text, rect)
            else:
                msg = f"Game Over! Score: {self.score}"
                text = self.font.render(msg, True, (255, 255, 0))
                rect = text.get_rect(center=(self.width // 2, y_center - 40))
                self.screen.blit(text, rect)
                diff = self.high_score - self.score
                diff_surface = self.small_font.render(
                    f"Keep working hard~ You are still {diff} away from the high score!", True, (255, 128, 0)
                )
                diff_rect = diff_surface.get_rect(center=(self.width // 2, y_center + 10))
                self.screen.blit(diff_surface, diff_rect)
            replay_surface = self.small_font.render("1: Replay", True, (255, 255, 255))
            exit_surface = self.small_font.render("2: Exit", True, (255, 255, 255))
            replay_rect = replay_surface.get_rect(center=(self.width // 2 - 100, y_center + 90))
            exit_rect = exit_surface.get_rect(center=(self.width // 2 + 100, y_center + 90))
            self.screen.blit(replay_surface, replay_rect)
            self.screen.blit(exit_surface, exit_rect)
            pygame.display.flip()
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    pygame.quit()
                    sys.exit()
                elif event.type == pygame.KEYDOWN:
                    if event.key in [pygame.K_1, pygame.K_KP1]:
                        return "replay"
                    elif event.key in [pygame.K_2, pygame.K_KP2]:
                        return "exit"
                    else:
                        # Input validation: show a message for invalid input
                        invalid_surface = self.small_font.render(
                            "Invalid input! Please press 1 or 2.", True, (255, 0, 0)
                        )
                        invalid_rect = invalid_surface.get_rect(center=(self.width // 2, y_center + 140))
                        self.screen.blit(invalid_surface, invalid_rect)
                        pygame.display.flip()
                        pygame.time.delay(1000)
                elif event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
                    mouse_pos = pygame.mouse.get_pos()
                    if replay_rect.collidepoint(mouse_pos):
                        return "replay"
                    elif exit_rect.collidepoint(mouse_pos):
                        return "exit"